/*****************************************************************************//*!
* file  : systask.c
* creat : apleilx
* note :  除初始化外，禁止该任务及其子程序使用含有阻塞的功能，包括数据存储，USB操作等等
* data  : 2019/07/02
*******************************************************************************/
#include "bsp.h"
#include "os_obj.h"
#include "bsp_spi_nor.h"
#include "systask.h"
#include "common.h"
#include "file_afat_tab.h"

#include "bsp_api.h"
#include "bsp_spi_nor.h"
#include "nor_cmd.h"

#include "nand_lut.h"
#include "nand_w25n.h"

#include "ff.h"
#include "simp_fat.h"
#include "swap.h"

#include "mbedtls/aes.h"
#include "mbedtls/sha256.h"
#include "mbedtls/md5.h"

#include "fat_opt.h"

/*****************************************************************************
* @brief   const define
*****************************************************************************/
#define TAB_ADDR					0x800000

uint8_t dat_from_sd_to_simp_fat(void);
void simp_fat_update_manage(void);

uint8_t simp_fat_file_read(void);

void afat_tab_save(void);
void afat_tab_read(void);
uint8_t afat_hal_write(uint32_t addr, int32_t len, void *pdat);
uint8_t afat_hal_read(uint32_t addr, int32_t len, void *pdat);
uint8_t afat_hal_erase(uint32_t addr);
uint8_t afat_hal_flush(void);

afat_tab_type afat_tab[64];

const file_afat_cg_type afat_cfg =
{
	afat_tab,
	sizeof(afat_tab) / sizeof(afat_tab[0]) - 2,
	32,
	4096,
	8 * 1024 * 1024,
	afat_tab_save,
	afat_tab_read,
	afat_hal_write,
	afat_hal_read,
	afat_hal_erase,
	afat_hal_flush
};

file_afat_obj_type afat_obj = { 0 };

simp_fat_opt_type  sfat_opt;

/*****************************************************************************
* @brief   storage task.
* @param   none
* @return  non
* @ Pass/ Fail criteria: none
*****************************************************************************/
void afat_task(void)
{
	// nor 初始化
	bsp_qpi_port_init();
	nor_spi_init();
	nor_init(1);

	// nand 初始化
	w25n_init();
	w25n_main_mem_select();
	nand_lut_open();

	afat_obj.cfg = &afat_cfg;
	afat_init(&afat_obj);

	sfat_opt.buff = malloc(4096);

	nand_lut_addr_read(32 * nand_dev.page_main_size * nand_dev.page_per_block + 1024, 128, sfat_opt.buff);
	sfat_opt.buff[128] = 0;
	flash_dbg_puts((void *)sfat_opt.buff);

	while (1)
	{
		if(sys_st.str_module != 0)
		{
			break;
		}
		
		osDelay(10);
		
		simp_fat_update_manage();
		simp_fat_file_read();
	}

	free(sfat_opt.buff);
}

/*****************************************************************************
* @brief   afat tab r/w.
* @param   none
* @return  non
* @ Pass/ Fail criteria: none
*****************************************************************************/
void afat_tab_save(void)
{
	nor_sector_erase(TAB_ADDR);
	nor_q_write_multi(TAB_ADDR, sizeof(afat_tab), (void *)afat_tab);
};

void afat_tab_read(void)
{
	nor_q_read(TAB_ADDR, sizeof(afat_tab), (void *)afat_tab);
};

/*****************************************************************************
* @brief   afat hal.
* @param   none
* @return  non
* @ Pass/ Fail criteria: none
*****************************************************************************/
uint8_t afat_hal_write(uint32_t addr, int32_t len, void *pdat)
{
	nor_q_write_multi(addr, len, pdat);
	return 0;
}

uint8_t afat_hal_read(uint32_t addr, int32_t len, void *pdat)
{
	nor_q_read(addr, len, pdat);
	return 0;
}

uint8_t afat_hal_erase(uint32_t addr)
{
	nor_sector_erase(addr);
	return 0;
}

uint8_t afat_hal_flush(void)
{
	return 0;
}

